<?php

namespace app\admin\controller;


use app\admin\common\Base;

class index extends Base {
    /**
     * 后台首页
     * @return mixed
     */
    public function index(){
        $logo = db('site')->find()['logo'];
        $this->assign('logo',$logo);
        return $this->fetch();
    }

    /**
     * 清理缓存
     */
    public function clearAll() {
        $cache_path = config('cache.runtime_path').'/cache/';
        $temp_path = config('cache.runtime_path').'/temp/';
        $log_path = config('cache.runtime_path').'/log/';
        chmod($cache_path,0777);
        chmod($temp_path,0777);
        chmod($log_path,0777);
        if (delete_dir_file($cache_path) && delete_dir_file($temp_path) && delete_dir_file($log_path)) {
            $this->success('清除缓存成功!');
        } else {
            $this->error('清除缓存失败!');
        }
    }


    public function welcome(){
//        phpinfo();
        $webServer = strtolower($_SERVER['SERVER_SOFTWARE']);
        if (strpos($webServer, 'apache') !== false) {
            $server_software = 'Apache';
        } elseif (strpos($webServer, 'microsoft-iis') !== false) {
            $server_software = 'IIS';
        } elseif (strpos($webServer, 'nginx') !== false) {
            $server_software = 'Nginx';
        } elseif (strpos($webServer, 'lighttpd') !== false) {
            $server_software = 'Lighttpd';
        } elseif (strpos($webServer, 'kangle') !== false) {
            $server_software = 'Kangle';
        } elseif (strpos($webServer, 'caddy') !== false) {
            $server_software = 'Caddy';
        } elseif (strpos($webServer, 'development server') !== false) {
            $server_software = 'Development Server ';
        } else {
            $server_software = $webServer;
        }

        $server = [
            'php_version' => phpversion(),
            'database' => config('database.type'),
            'server_software' => $server_software,
            'ext_file_info' => extension_loaded('fileinfo') == 1 ? true : false,
            'ext_openssl' => extension_loaded('openssl') == 1 ? true : false,
            'os' => php_uname('s')
        ];
        $this->assign('server_information',$server);

        $this->assign('time',date('Y-m-d H:i:s',time()));

        $this->assign('product_type',$this->getCount('category_product'));
        $this->assign('product',$this->getCount('product'));
        $this->assign('news',$this->getCount('news'));
        $this->assign('solution',$this->getCount('solution'));
        $this->assign('inquiry',$this->getCount('inquiry'));
        $this->assign('not_read_inquiry',$this->notReadMessage());
        $this->assign('pv',$this->getLastThirtyDaysPv());
        $this->assign('last_thirty_day',$this->getLastThirtyDays());

        return $this->fetch();
    }

    public function getCount($table){
        $count = db($table)
            ->where('is_del',0)
            ->count();
        return $count;
    }

    private function notReadMessage(){
        $list = db('inquiry')
            ->where('is_del',0)
            ->where('status',0)
            ->select();
        return $list;
    }

    /**
     * 获取最近7天日期
     * @return false|string
     */
    public function getLastSevenDay(){
        $day_1 = date('m-d',time() - 24 * 60 * 60 * 6);
        $day_2 = date('m-d',time() - 24 * 60 * 60 * 5);
        $day_3 = date('m-d',time() - 24 * 60 * 60 * 4);//...
        $day_4 = date('m-d',time() - 24 * 60 * 60 * 3);//大前天
        $day_5 = date('m-d',time() - 24 * 60 * 60 * 2);//前天
        $day_6 = date('m-d',time() - 24 * 60 * 60);//昨天
        $today = date('m-d');//今天

        return json_encode([$day_1,$day_2,$day_3,$day_4,$day_5,$day_6,$today]);
    }
    /**
     * 获取最近30天日期
     * @return false|string
     */
    public function getLastThirtyDays(){
        $days = [];
        for ($i = 29; $i >= 0; $i--) {
            $days[] = date('m-d', time() - 24 * 60 * 60 * $i);
        }
        return json_encode($days);
    }
    /**
     * 获取最近7天站点访问量
     * @return false|string
     */
    public function getLastSevenDaysPv(){
        $day_1 = [
            'start' => strtotime(date('Y-m-d',strtotime("-6 day"))),
            'end' => strtotime(date('Y-m-d',strtotime("-5 day")))
        ];
        $day_2 = [
            'start' => strtotime(date('Y-m-d',strtotime("-5 day"))),
            'end' => strtotime(date('Y-m-d',strtotime("-4 day")))
        ];
        $day_3 = [
            'start' => strtotime(date('Y-m-d',strtotime("-4 day"))),
            'end' => strtotime(date('Y-m-d',strtotime("-3 day")))
        ];
        //大前天
        $day_4 = [
            'start' => strtotime(date('Y-m-d',strtotime("-3 day"))),
            'end' => strtotime(date('Y-m-d',strtotime("-2 day")))
        ];
        //前天
        $day_5 = [
            'start' => strtotime(date('Y-m-d',strtotime("-2 day"))),
            'end' => strtotime(date('Y-m-d',strtotime("-1 day")))
        ];
        //昨天
        $day_6 = [
            'start' => strtotime(date('Y-m-d',strtotime("-1 day"))),
            'end' => strtotime(date('Y-m-d'))
        ];
        //今天
        $today = [
            'start' => strtotime(date('Y-m-d')),
            'end' => strtotime(date('Y-m-d',strtotime("+1 day")))
        ];

        $day_1_count = 0;
        $day_2_count = 0;
        $day_3_count = 0;
        $day_4_count = 0;
        $day_5_count = 0;
        $day_6_count = 0;
        $today_count = 0;

        $pv = db('pv')->column('create_time');
        if (!empty($pv)){
            foreach ($pv as $k => $v) {
                if ($v > $day_1['start'] && $v < $day_1['end']) {
                    $day_1_count++;
                } elseif ($v > $day_2['start'] && $v < $day_2['end']) {
                    $day_2_count++;
                } elseif ($v > $day_3['start'] && $v < $day_3['end']) {
                    $day_3_count++;
                } else if ($v > $day_4['start'] && $v < $day_4['end']) {
                    $day_4_count++;
                } else if ($v > $day_5['start'] && $v < $day_5['end']) {
                    $day_5_count++;
                } else if ($v > $day_6['start'] && $v < $day_6['end']) {
                    $day_6_count++;
                } else if ($v > $today['start'] && $v < $today['end']) {
                    $today_count++;
                }
            }
        }

        return json_encode([$day_1_count,$day_2_count,$day_3_count,$day_4_count,$day_5_count,$day_6_count,$today_count]);
    }

    /**
     * 获取最近30天站点访问量
     * @return false|string
     */
    public function getLastThirtyDaysPv(){
        $days = [];

        // 获取最近30天的日期范围
        for ($i = 29; $i >= 0; $i--) {
            $start = strtotime("-{$i} day", strtotime(date('Y-m-d')));
            $end = strtotime("+1 day", $start);
            $days[] = ['start' => $start, 'end' => $end];
        }

        // 初始化每天的访问量为0
        $day_counts = array_fill(0, 30, 0);

        $pv = db('pv')->column('create_time');
        if (!empty($pv)){
            foreach ($pv as $v) {
                // 检查访问时间属于哪一天，并增加对应天数的访问量计数
                foreach ($days as $key => $day) {
                    if ($v >= $day['start'] && $v < $day['end']) {
                        $day_counts[$key]++;
                        break;
                    }
                }
            }
        }

        return json_encode($day_counts);
    }
}